package com.example.emergencyattempt01.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.administrator_activity.Update_Book_Activity;

import java.util.List;
import java.util.Map;

public class Ad_BookAdapter extends BaseAdapter {
    Context context;
    List<Map<String,Object>> itemList;
    ViewHolder viewHolder=null;




    class ViewHolder
    {
        TextView ISBNField;
        TextView bookNameField;
        TextView authorField;
        TextView pressField;
        TextView sumNumberField;
        TextView nowNumberField;
        TextView typeField;
        TextView introductionField;

        Button updateBook;

    }

    public Ad_BookAdapter(Context context, List<Map<String, Object>> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public String getISBN(int position)
    {
        return (String)itemList.get(position).get("ISBN");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LayoutInflater inflater=LayoutInflater.from(context);
        if(convertView==null)
        {
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.ad_book_item,null);
            viewHolder.authorField=convertView.findViewById(R.id.ad_book_author);
            viewHolder.bookNameField=convertView.findViewById(R.id.ad_book_name);
            viewHolder.introductionField=convertView.findViewById(R.id.ad_book_introduction);
            viewHolder.ISBNField=convertView.findViewById(R.id.ad_book_ISBN);
            viewHolder.nowNumberField=convertView.findViewById(R.id.ad_book_nowNumber);
            viewHolder.sumNumberField=convertView.findViewById(R.id.ad_book_sumNumber);
            viewHolder.pressField=convertView.findViewById(R.id.ad_book_press);
            viewHolder.typeField=convertView.findViewById(R.id.ad_book_type);
            viewHolder.updateBook=convertView.findViewById(R.id.book_info_update);

           convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        // 提取数据
        final String ISBN;
        final String bookName;
        final String author;
        final String press;
        final long sumNumber;
        final long nowNumber;
        final String type;
        final String introduction;

        ISBN=(String)itemList.get(position).get("ISBN");
        bookName=(String)itemList.get(position).get("bookName");
        author=(String)itemList.get(position).get("author");
        press=(String)itemList.get(position).get("press");
        introduction=(String)itemList.get(position).get("introduction");
        type=(String)itemList.get(position).get("type");
        sumNumber=(long)itemList.get(position).get("sumNumber");
        nowNumber=(long)itemList.get(position).get("nowNumber");
        // 管理员查看，没有任何限制，因此全部展示

        // 文字部分
        viewHolder.ISBNField.setText(ISBN);
        viewHolder.bookNameField.setText(bookName);
        viewHolder.typeField.setText(type);
        viewHolder.pressField.setText(press);
        viewHolder.sumNumberField.setText(sumNumber+"册");
        viewHolder.nowNumberField.setText(nowNumber+"册");
        viewHolder.authorField.setText(author);
        viewHolder.introductionField.setText(introduction);
        // 设置监听
        viewHolder.updateBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, Update_Book_Activity.class);
                intent.putExtra("ISBN",ISBN);
                intent.putExtra("bookName",bookName);
                intent.putExtra("type",type);
                intent.putExtra("press",press);
                intent.putExtra("sumNumber",sumNumber);
                intent.putExtra("nowNumber",nowNumber);
                intent.putExtra("author",author);
                intent.putExtra("introduction",introduction);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

}
